#!/bin/bash
#
# Copyright (c) 2007 OLPC
# Author: gxlxnis@lxptop.org | sed s/x/a/
# Version 0.5
#
# this program is free software; you can redistribute it and/or modify
# it under the terms of the gnu general public license as published by
# the free software foundation; either version 2 of the license, or
# (at your option) any later version.
#
# this program is distributed in the hope that it will be useful,
# but without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose.  see the
# gnu general public license for more details.
#
# you should have received a copy of the gnu general public license
# along with this program; if not, write to the free software
# foundation, inc., 51 franklin st, fifth floor, boston, ma  02110-1301  usa

timestamp=`date +20%y-%m-%d.%H-%M-%S`
serial=`cat /ofw/serial-number`
target="logtemp"
sugarpath="/home/olpc/.sugar/default/logs"

function general {
	[ -d $target ] && rm -r logtemp
	mkdir $target

	olpc-netstatus -i &> $target/info;echo -n =
}

function sugar {
	mkdir $target/sugar
	cp /home/olpc/.sugar/default/logs/*.log $target/sugar;echo -n ===
}

function kernel {
	mkdir $target/kernel
	cp /var/log/messages $target/kernel;echo -n 
	cp /var/log/Xorg.0.log $target/kernel;echo -n = 
	dmesg > $target/kernel/dmesg.out;echo -n =
}

function net {
	mkdir $target/net	
	
	cat /home/olpc/.sugar/default/config &> $target/net/config;echo -n 
	cat /etc/resolv.conf &> $target/net/resolv.conf;echo -n
	cat /home/olpc/.sugar/default/nm/networks.cfg &> $target/net/networks.cfg;echo -n 
	cat /etc/NetworkManager/mesh-start &> $target/net/mesh-start;echo -n =

	ifconfig &> $target/net/ifconfig.out;echo -n =
	iwconfig &> $target/net/iwconfig.out;echo -n =
	route -n &> $target/net/route.out;echo -n =
	ps auxf &> $target/net/ps.out;echo -n =
	netstat -apT &> $target/net/netstat.out;echo -n ==

	olpc-netstatus -n &> $target/net/olpc-netstatus.out;echo -n ===
	olpc-mesh &> $target/net/olpc-mesh.out;echo -n ==
	olpc-xos sugar &> $target/net/xos;echo -n ==
}

function help {
cat <<EOF
Usage: olpc-netlog [OPTION]
Store important logs in a tarball named by time and serial number.
Specify which logs to store using the options. Default is all logs.

Options:
  -n     network logs              :  ifconfig output
                                      iwconfig output
                                      route output
                                      ps -auxf output
                                      netstat -apT output
                                      olpc-netstatus output
                                      olpc-mesh output
                                      presenceservice XO list

                                      /home/olpc/.sugar/default/config
                                      /etc/resolv.conf
                                      /home/olpc/.sugar/default/nm/networks.cfg
                                      /etc/NetworkManager/mesh-start

  -s     sugar and activities logs :  $sugarpath/datastore.log
                                      $sugarpath/telepathy-salut.log
                                      $sugarpath/telepathy-gabble.log
                                      $sugarpath/presenceservice.log
                                      $sugarpath/shellservice.log
                                      $sugarpath/shell.log
                                      all activity logs

  -k     kernel related logs       :  dmesg output
                                      /var/log/messages
                                      /var/log/Xorg.0.log

EOF
}

if [ "$1" = "--help" ]; then
	help
	exit 0
fi

param=`echo " $*"|sed s/" "//g|sed s/-//g`

echo -ne "Collecting: [                      ]\033[23D"

general

if [ -z "$param" ]; then kernel;sugar;net
elif ! echo " $param"|grep -E "(n|k|s)" > /dev/null
then 
	echo "Warning: all logs were selected"
	echo "For help use 'olpc-netlog' --help"
	sugar;kernel;net 
else

	echo "$param"|grep n > /dev/null && sugar
	echo "$param"|grep k > /dev/null && kernel
	echo "$param"|grep s > /dev/null && net

	rest=`echo $param|sed s/n//|sed s/s//|sed s/k//`
	if [ -n "$rest" ];then
		echo -e "Warning: Parameters \"$rest\" were ignored"
		echo "For help use 'olpc-netlog --help'"
	fi
fi	

cd $target
tar -cjf ../logs.$serial.$timestamp.tar.bz2 *;echo -n ==
cd ../

rm -r logtemp

echo -e "\033[1C"
